import { Component, OnInit } from '@angular/core';
import {ToBeEvaluatedService} from './to-be-evaluated.service';
import {NzMessageService, scrollIntoView} from 'ng-zorro-antd';
import {PageEntity} from '../../../core/domain/page.entity';
import {StatusEnum} from '../../../core/enum/status.enum';
import {PaginationUtils} from '../../../utils/paginationUtils';
import {FilterEnum} from '../../../core/enum/filter.enum';

@Component({
  selector: 'app-to-be-evaluated',
  templateUrl: './to-be-evaluated.component.html',
  styleUrls: ['./to-be-evaluated.component.scss'],
  providers: [ToBeEvaluatedService]
})
export class ToBeEvaluatedComponent implements OnInit {
  pageInfo: PageEntity = new PageEntity();
  yearList = [];
  semesterList = [];
  searchParams: SearchParams = new SearchParams();
  dataList = [];
  view = false;
  evaluateList: any = [];
  totalQuestionArr = [];
  answeredIndexArr = [];
  evaluateObj: any = {};
  nzWidthConfigs = [];
  visible = false;
  visibleSubmit = false;
  constructor(
    private http: ToBeEvaluatedService,
    private message: NzMessageService
  ) { }

  ngOnInit() {
    this.getSchoolYearList();
  }


  page(page) {
    this.pageInfo.filters.state = PaginationUtils.filters('待评价', FilterEnum.EQUALS);
    this.pageInfo.multiSortMeta = [
      {
        field: 'gmtCreate',
        order: -1
      }
    ];
    this.http.page(page).subscribe(res => {
     if (res.status === StatusEnum.SUCCESS) {
       this.dataList = res.datas.data;
       this.pageInfo.totalRecords = res.datas.totalRecords;
       this.nzWidthConfigs = ['60px', '150px', '100px', '150px', '150px',
         '100px', '100px', '180px', '100px', '100px',
         '300px', '150px',
         '200px', '100px'];
     }
    });
  }

  /**
   * 获取学年学期
   */
  getSchoolYearList() {
    this.http.getSchoolYearApi().subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.yearList = res.datas;
        res.datas.map(item => {
          if (item.currSchoolYear) {
            // this.searchParams.schoolYear = item.schoolYear;
            this.semesterList = item.semester;
            item.semester.map(ele => {
              if (ele.currSemester) {
                // this.searchParams.semester = ele.semester;
              }
            });
          }
        });
        this.clickSearch();
      }
    });
  }

  /**
   * 点击查询
   */
  clickSearch() {
    for (const o in this.searchParams) {
      if (this.searchParams[o]) {
        this.pageInfo.filters[o] = PaginationUtils.filters(this.searchParams[o], FilterEnum.CONTAINS);
      } else {
        delete this.pageInfo.filters[o];
      }
      if (this.searchParams.semester === 0) {
        this.pageInfo.filters.semester = PaginationUtils.filters(this.searchParams.semester, FilterEnum.EQUALS);
      }
    }
    this.pageInfo.first = 1;
    this.page(this.pageInfo);
  }

  /**
   * 点击重置
   */
  resetSearch() {
    this.searchParams = new SearchParams();
    this.pageInfo = new PageEntity();
    this.clickSearch();
  }

  changePageIndex(index) {
    this.pageInfo.first = index;
   this.page(this.pageInfo);
  }

  changePageSize(size) {
    this.pageInfo.first = 1;
    this.pageInfo.rows = size;
    this.clickSearch();
  }

  /**
   * 点击评价
   */
  clickEvaluate(data) {
    this.http.getEvaluateApi({
      id: data.id
    }).subscribe(res => {
     if (res.status === StatusEnum.SUCCESS) {
       this.view = true;
       this.totalQuestionArr = [];
       this.answeredIndexArr = [];
       this.evaluateObj = res.datas;
       this.evaluateList = res.datas.detailList;
       for (let i = 0; i < this.evaluateList.length; i++) {
         this.totalQuestionArr.push(i);
       }
     }
    });
  }

  /**
   * 当问题回答时
   */
  changeQuestionIndex(index, selectList, value) {
    if (!this.answeredIndexArr.includes(index)) {
      this.answeredIndexArr.push(index);
    }
    if (!value) {
      let delIndex;
      this.answeredIndexArr.map((item, i) => {
        if (item === index) {
          delIndex = i;
        }
      });
      this.answeredIndexArr.splice(delIndex, 1);
    }
    let inputFlag = false;
    (selectList || []).map(item => {
      if (item.id === value && item.name === '差') {
        inputFlag = true;
      }
    });
    this.evaluateList[index].inputFlag = inputFlag;
  }

  /**
   * 点击答题卡目录，滚动到题目位置
   */
  clickAnswerIndex(index) {
    document.getElementById('questionItem-' + index).scrollIntoView({
      behavior: 'smooth'
    });
  }

  /**
   * 点击提交
   */
  submitEvaluate() {
    let flag = false;
    this.evaluateObj.detailList.map(item => {
      if (item.examQuestionCate === '简答题') {
        if (!item.content) {
          flag = true;
        }
      } else {
        if (!item.examSelectId) {
          flag = true;
        }
        (item.selectList || []).map(ele => {
          if (item.examSelectId === ele.id && ele.name === '差' && !item.content) {
            flag = true;
          }
        });
      }
    });
    if (flag) {
      this.visible = true;
    } else {
      this.visibleSubmit = true;
    }
  }

  clickConfirm() {
    this.http.submitEvaluateApi(this.evaluateObj).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.view = false;
        this.visibleSubmit = false;
        this.page(this.pageInfo);
      }
    });
  }
}
class SearchParams {
  schoolYear: string;
  semester: any;
  number: string;
  name: string;
}
